package data;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public abstract class AbstractData {
	
	protected static DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	protected transient Key key = null;
	
	public abstract Entity getEntity();
	public abstract void setEntity(Entity entity);
	
	public long getId()
	{
		return -1;
	}
	
	public void setKey(Key key){
		this.key = key;
	}
	
	protected String CombineList(List<?> list)
	{
	
		String rs = "";
		if(list == null)
			return rs;
		for(int i=0;i<list.size();i++)
		{
			rs += ((AbstractData) list.get(i)).getId();
			if(i != list.size()-1)
				rs += ",";
		}
		return rs;
	}
	
	protected String[] SplitList(String stringList)
	{
		return stringList.split(",");
	}
	protected Entity LoadData(String entityName,long id)
	{
		Key key = KeyFactory.createKey(entityName, id);
		try {
			return datastore.get(key);
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	public static Date addDays(Date d, long days)
    {
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.add(Calendar.DATE, (int)days);
		return c.getTime();
    }
}
